package com.leo;

import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.level.Level;
import com.almasb.fxgl.input.UserAction;
import com.leo.entity.MapEntityFactory;
import javafx.scene.input.KeyCode;

import java.util.Map;

/**
 * @Author: wangss
 * @CreateTime: 2023-06-18  23:23
 * @Description: 地图编辑器
 */
public class MapApp extends GameApplication {
    @Override
    protected void initSettings(GameSettings settings) {

    }

    @Override
    protected void initGameVars(Map<String, Object> vars) {
        vars.put("level", 1);
    }

    @Override
    protected void initInput() {
        FXGL.getInput().addAction(new UserAction("上一关") {
            @Override
            protected void onActionBegin() {
                if (FXGL.geti("level") > 1) {
                    FXGL.inc("level", -1);
                    FXGL.setLevelFromMap("map" + FXGL.geti("level") + ".tmx");
                } else {
                    FXGL.getGameScene().getViewport().shakeTranslational(100);
                }
            }
        }, KeyCode.UP);

        FXGL.getInput().addAction(new UserAction("下一关") {
            @Override
            protected void onActionBegin() {
                if (FXGL.geti("level") < 2) {
                    FXGL.inc("level", 1);
                    FXGL.setLevelFromMap("map" + FXGL.geti("level") + ".tmx");
                } else {
                    FXGL.getGameScene().getViewport().shakeTranslational(10);
                }
            }
        }, KeyCode.DOWN);
    }

    @Override
    protected void initGame() {
        FXGL.getGameWorld().addEntityFactory(new MapEntityFactory());

        Level level = FXGL.setLevelFromMap("map1.tmx");
        // 获取地图属性
        String level1 = level.getProperties().getString("level");
        System.out.println("level=" + level1);
        Integer max = level.getProperties().getInt("max");
        System.out.println("max=" + max);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
